//
//
// 定义防抖函数
export const debounce = (func: Function, delay: number) => {
  let timer: number
  return function (...args: any[]) {
    clearTimeout(timer)
    // @ts-ignore
    timer = setTimeout(() => {
      // @ts-ignore
      func.apply(this, args)
    }, delay)
  }
}

//
//
// 定义节流函数
export const throttle = (func: Function, delay: number) => {
  let timer: number | null = null
  return function (...args: any[]) {
    if (!timer) {
      // @ts-ignore
      timer = setTimeout(() => {
        // @ts-ignore
        func.apply(this, args)
        timer = null
      }, delay)
    }
  }
}

//
//
// 获取上传的文件原始文件
export const fetchOriginFile = (file, key?: string) => {
  const reader = new FileReader()
  // 读取完成后，将文件原始数据存储到 file.raw 属性中
  //  @ts-ignore
  reader.onload = (event) => (file.raw = event.target.result)
  reader.readAsDataURL(file)

  if (!key) return file

  let formdata = new FormData()
  formdata.append(key, file)
  return formdata
}
